/**
 * @Title: login
 * @Package mall-fe
 * @author Shinelon
 * @date 2017/9/3 13:23
 * @version V1.0
 */
"use strict";
require('./index.css');
require('js/common/nav-simple/index');
var shiqla = require('util/shiqlamall');
var userService = require('../../service/user-service');
/**
 * 登录页面逻辑
 * @type {{init: page.init}}
 */
var formError = {
    show: function (errMsg) {
        $('.error-item').show().find('.err-msg').text(errMsg);
    },
    hide: function () {
        $('.error-item').hide().find('.err-msg').text('');
    }
};

var page = {
    data: {
        username: '',
        question: '',
        token: '',
        answer: ''
    },
    //初始化密码找回第一步页面
    init: function () {
        this.onload();
        this.bindEvent();
    },
    //加载第一步页面
    onload: function () {
        //显示第一步
        this.loadStepUsername();
    },
    bindEvent: function () {
        var _this = this;
        //第一步输入用户名点击下一步
        $('#submit-username').click(function () {
            var username = $.trim($('#username').val());
            if (username) {
                //如果获取问题成功,则需要隐藏第一步,显示第二步
                _this.data.username = username;
                userService.getQuestion(_this.data, function (result) {
                    _this.data.question = result;
                    _this.loadStepAswer();
                }, function (errMsg) {
                    formError.show(errMsg);
                })
            }
            else {
                formError.show('请输入用户名!');
            }
        });
        //第二步输入提示问题答案,点击下一步
        $('#submit-question').click(function(){
            var answer = $.trim($('#step-question').val());
            if(answer){
                userService.checkAnswer({
                    username:_this.data.username,
                    question:_this.data.question,
                    answer:answer
                },function(result){
                    _this.data.answer   = answer;
                    _this.data.token    = result;
                    _this.loadStepPassword();
                },function(errMsg){
                     formError.show(errMsg);
                });
            }
        });

        //第三步,提交新的密码
        $('#submit-password').click(function(){
            var password = $.trim($('#step-password').val());
            if(password){
                //提交新密码数据到服务器
                userService.resetPassword({
                    username:_this.data.username,
                    password:password,
                    forgetToken:_this.data.token
                },function(reslut){
                     window.location.href = './result.html';
                },function(errMsg){
                    formError.show(errMsg);
                });
            }
        });
    },
    //加载输入用户名第一步
    loadStepUsername: function () {
        $('.step-username').show();
    },
    //加载输入密保问题第二步
    loadStepAswer: function () {
      //清除错误提示
      formError.hide();
      //切换容器到第二步
      $('.step-username').hide();
      $('.step-question').show().find('.question').text(this.data.question);
    },
    //输入新密码第三步
    loadStepPassword: function () {
        formError.hide();
      //切换容器到第二步
      $('.step-question').hide();
      $('.step-password').show();
    }
};
$(function () {
    page.init();
});